package com.mingrisoft;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class limitSettingActivity extends Activity implements View.OnClickListener {
    private EditText edtv1, edtv2, edtv3, edtv4, edtv5;
    private EditText edtv6, edtv7, edtv8, edtv9, edtv10;
    private Button editBtn, appBtn;
    private TextView msgtv;
    private mqttManager mqtt_Manager;
    private String publishTopic="controlDevice";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_limit_setting);
        initView();
        initData();
        TextView[] tvs = {};
        mqtt_Manager = mqttManager.getInstance(tvs, 0);
        mqtt_Manager.subscribeMessage(publishTopic,1);
        editBtn.setOnClickListener(this);
        appBtn.setOnClickListener(this);
    }

    private void initView() {
        // TODO Auto-generated method stub
        edtv1 = (EditText) findViewById(R.id.tempLimitUp);
        edtv2 = (EditText) findViewById(R.id.ahumLimitUp);
        edtv3 = (EditText) findViewById(R.id.shumLimitUp);
        edtv4 = (EditText) findViewById(R.id.co2LimitUp);
        edtv5 = (EditText) findViewById(R.id.lightLimitUp);
        edtv6 = (EditText) findViewById(R.id.tempLimitDown);
        edtv7 = (EditText) findViewById(R.id.ahumLimitDown);
        edtv8 = (EditText) findViewById(R.id.shumLimitDown);
        edtv9 = (EditText) findViewById(R.id.co2LimitDown);
        edtv10 = (EditText) findViewById(R.id.lightLimitDown);
        editBtn = (Button) findViewById(R.id.enableSetBtn2);
        appBtn = (Button) findViewById(R.id.appBtn2);
        msgtv = (TextView) findViewById(R.id.msgshowtv);
    }

    private void initData() {


    }

    public void onClick(View view) {
        if (view.getId() == R.id.enableSetBtn2) {
            edtv1.setEnabled(true);
            edtv2.setEnabled(true);
            edtv3.setEnabled(true);
            edtv4.setEnabled(true);
            edtv5.setEnabled(true);
            edtv6.setEnabled(true);
            edtv7.setEnabled(true);
            edtv8.setEnabled(true);
            edtv9.setEnabled(true);
            edtv10.setEnabled(true);
        } else if (view.getId() == R.id.appBtn2) {
            int count=0;
            count+=1;
            String msg, TempUp, TempDown, AHumUp, AHumDown, SHumUp, SHumDown, LightUp, LightDown, CO2Up, CO2Down;
            TempUp = edtv1.getText().toString();
            AHumUp = edtv2.getText().toString();
            SHumUp = edtv3.getText().toString();
            LightUp = edtv4.getText().toString();
            CO2Up = edtv5.getText().toString();
            TempDown = edtv6.getText().toString();
            AHumDown = edtv7.getText().toString();
            SHumDown = edtv8.getText().toString();
            LightDown = edtv9.getText().toString();
            CO2Down = edtv10.getText().toString();
            msg="{"+"\"seq\":"+count+",\"type\":2"+",\"TempUp\":"+TempUp+",\"TempDown\":"+TempDown+",\"AHumUp\":"+AHumUp+",\"AHumDown\":"+AHumDown+",\"SHumUp\":"+SHumUp+",\"SHumDown\":"+SHumDown+",\"LightUp\":"+LightUp+",\"LightDown\":"+LightDown+",\"CO2Up\":"+CO2Up+",\"CO2Down\":"+CO2Down+"}";
            mqtt_Manager.publishMessage(publishTopic,msg);
            msgtv.append(msg+"\n");
        }
    }

}
